package com.sgb.auction.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.auction.R;
import com.sgb.auction.databinding.ActivityAuctionSkulistBinding;
import com.sgb.link.entity.GetSkuEntity;
import com.sgb.link.entity.GoodsSkuChild;
import com.sgb.auction.adapter.AuctionSkuAdaptrer;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import java.io.Serializable;
import java.util.List;

/**
 * Time:2021/11/29
 * AUTHOR:F_Q
 * Description:
 */
public class AuctionSkuListActivity  extends AppActivity {

    private ActivityAuctionSkulistBinding mBinding;
    private List<GetSkuEntity> skuList;
    private AuctionSkuAdaptrer adapter;

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_auction_skulist);
        skuList = (List<GetSkuEntity>) getIntent().getSerializableExtra("data");
        adapter = new AuctionSkuAdaptrer(this, skuList);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<GetSkuEntity>() {
            @Override
            public void onItemClick(GetSkuEntity getSkuEntity, int position) {
                if (getSkuEntity.getList()!=null&&getSkuEntity.getList().size()>0){
                    showListPop("请选择"+getSkuEntity.getParamData(),getSkuEntity.getList(),position);
                }else {
                    MToast.showToast(getActivity(),"没有"+getSkuEntity.getParamData()+"子属性");
                }
            }
        });
        mBinding.includeTitle.titleView.setText("商品规格");
        mBinding.includeTitle.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    private void initData() {
        for (GetSkuEntity data : adapter.getDatas()) {
            if (TextUtils.isEmpty(data.getText())){
                MToast.showToast(this,"请选择"+data.getParamData());
                return;
            }
        }
        Intent intent = new Intent();
        intent.putExtra("data", (Serializable) adapter.getDatas());
        setResult(RESULT_OK, intent);
        finish();
    }

    private void showListPop(String title, List<GoodsSkuChild> data, int pos) {
        if (data != null && data.size() > 0) {
            OptionsPickerView<GoodsSkuChild> mCustomerOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    adapter.getDatas().get(pos).setText(data.get(options1).getPickerViewText());
                    adapter.getDatas().get(pos).setTextid(data.get(options1).getValId());
                    adapter.notifyDataSetChanged();
                }
            }).setCancelColor(Color.parseColor("#666666"))
                    .setSubCalSize(15)
                    .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                    .setSubmitColor(Color.parseColor("#5792FD")).build();
            mCustomerOptions.setTitleText(title);
            mCustomerOptions.setPicker(data);
            mCustomerOptions.setSelectOptions(0);
            mCustomerOptions.show();
        }
    }
}
